/**
 *
 *emlog.hpp.
 *
 * @remarks
 * 2023 created by ycl.
 */

#ifndef _EMLOG_HPP_
#define _EMLOG_HPP_

#include <iostream>
#include <stdarg.h>
#include <stdio.h>
#include <time.h>

class EMLog
{
public:
	static const int LogLevelError = 0;
	static const int LogLevelWarning = 1;
	static const int LogLevelInfo = 2;
    static const int LogLevelDebug = 3;

private: // 默认的level是InfoLevel
	int m_LogLevel = LogLevelInfo; // class member private
    std::string m_moduleName ="";
    char m_sprintf_buf[1024];

public:
    EMLog(std::string moduleName)
    {
        m_moduleName = moduleName;        
    }

    EMLog(std::string moduleName, int level)
    {
        m_moduleName = moduleName;
        m_LogLevel = level;
    }
    
	void SetLevel(int level)
	{
		m_LogLevel = level;
	}

    void Error(const char *fmt, ...)
	{
        va_list args;
		if (m_LogLevel >= LogLevelError)
		{
            time_t now = time(NULL);
            struct tm *t = localtime(&now);
            
            va_start(args, fmt);
            vsprintf(m_sprintf_buf, fmt, args);
            va_end(args);            
            printf("[%d-%d-%d %d:%d:%d][%s][Error]: %s\n", t->tm_year + 1900,t->tm_mon +1, t->tm_mday,
                    t->tm_hour, t->tm_min, t->tm_sec , m_moduleName.c_str(), m_sprintf_buf);
        }
	}

    void Warn(const char *fmt, ...)
	{
        va_list args;
		if (m_LogLevel >= LogLevelWarning)
		{
            time_t now = time(NULL);
            struct tm *t = localtime(&now);
            
            va_start(args, fmt);
            vsprintf(m_sprintf_buf, fmt, args);
            va_end(args);
            printf("[%d-%d-%d %d:%d:%d][%s][Warn]: %s\n", t->tm_year + 1900,t->tm_mon +1, t->tm_mday,
                    t->tm_hour, t->tm_min, t->tm_sec , m_moduleName.c_str(), m_sprintf_buf);
        }
	}

    void Info(const char *fmt, ...)
	{       
        va_list args; 
		if (m_LogLevel >= LogLevelInfo)
		{            
            time_t now = time(NULL);
            struct tm *t = localtime(&now);
            
            va_start(args, fmt);
            vsprintf(m_sprintf_buf, fmt, args);
            va_end(args);
            printf("[%d-%d-%d %d:%d:%d][%s][Info]: %s\n", t->tm_year + 1900,t->tm_mon +1, t->tm_mday,
                    t->tm_hour, t->tm_min, t->tm_sec , m_moduleName.c_str(), m_sprintf_buf);
        }
	}

    void Debug(const char *fmt, ...)
	{       
        va_list args; 
		if (m_LogLevel >= LogLevelDebug)
		{
            time_t now = time(NULL);
            struct tm *t = localtime(&now);
            
            va_start(args, fmt);            
            vsprintf(m_sprintf_buf, fmt, args);        
            va_end(args);
            printf("[%d-%d-%d %d:%d:%d][%s][Debug]: %s\n", t->tm_year + 1900,t->tm_mon +1, t->tm_mday,
                    t->tm_hour, t->tm_min, t->tm_sec , m_moduleName.c_str(), m_sprintf_buf);
        }
	}
};

#endif